<?php


class Zixun extends Admin_controller
{
	
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model( 'zixun_mdl', 'zixun' );
	}
	
	public function index()
	{
		$zixun = array();
		$zixun = $this->zixun->getZixunList();
		$data['zixun'] = $zixun;
		$this->load->view( 'adminroot/zixun',$data );
	}
	
	
	/**
	 * 
	 * 添加资讯
	 */
	public function add()
	{
		     
		$this->_load_validation_rules();
		if ( $this->form_validation->run() == FALSE ) {
			

			$this->load->library( 'fckeditor','content' );
			$this->fckeditor->InstanceName = 'content';
	        $this->fckeditor->ToolbarSet = 'Default';
	        $this->fckeditor->Height = 800;
	        $data['fckeditor']=$this->fckeditor->CreateHtml();
	        
	        $this->load->library( 'fckeditor','infos' );
			$this->fckeditor->InstanceName = 'infos';
	        $this->fckeditor->ToolbarSet = 'Default';
	        $this->fckeditor->Height = 300;
	        $data['fckeditor2']=$this->fckeditor->CreateHtml();
	        
	        $this->load->model( 'zixuncategory_mdl', 'zixuncategory' );
			$category = $this->zixuncategory->getZixunCategoryList( false );
			$data['category'] = $category;
			
			$this->load->view( 'adminroot/zixun_add', $data );
			
		} else {
			
			$data['title']      = $this->input->post( 'title' );
			$data['parentid']   = $this->input->post( 'parentid' );
			$data['rank']       = $this->input->post( 'rank' );
			$data['infos']      = $this->input->post( 'infos' );
			$data['content']    = $this->input->post( 'content' );
			$data['author']     = $this->input->post( 'author' );
			$data['createtime'] = time();
			
			if ( $this->zixun->add_zixun( $data) ) {
				
				redirect( 'adminroot/zixun' );
				
			}		
		}
       
	}
	
	/**
	 * 
	 * 修改
	 */
	
	public function edit()
	{
		$id = intval( $this->uri->rsegment(4) );
		$zixun = array();
		$zixun = $this->zixun->getZixunById( $id );
		
		$this->load->library( 'fckeditor','content' );
		$this->fckeditor->InstanceName = 'content';
	    $this->fckeditor->ToolbarSet = 'Default';
	    $this->fckeditor->Height = 800;
	     $this->fckeditor->Value = $zixun['content'];
	    $data['fckeditor']=$this->fckeditor->CreateHtml();
	        
	    $this->load->library( 'fckeditor','infos' );
		$this->fckeditor->InstanceName = 'infos';
	    $this->fckeditor->ToolbarSet = 'Default';
	    $this->fckeditor->Height = 300;
	    $this->fckeditor->Value = $zixun['infos'];
	    $data['fckeditor2']=$this->fckeditor->CreateHtml();
	        
	    $this->load->model( 'zixuncategory_mdl', 'zixuncategory' );
		$category = $this->zixuncategory->getZixunCategoryList( false );
		$data['category'] = $category;
		
		$data['zixun'] = $zixun;
		
		$this->load->view( 'adminroot/zixun_edit', $data );
		
		
	}
	
	/**
	 * 
	 * 修改
	 */
	
	public function update()
	{
			$this->_load_validation_rules();
		if ( $this->form_validation->run() == true ) {
			
			$data['title']      = $this->input->post( 'title' );
			$data['parentid']   = $this->input->post( 'parentid' );
			$data['rank']       = $this->input->post( 'rank' );
			$data['infos']      = $this->input->post( 'infos' );
			$data['content']    = $this->input->post( 'content' );
			$data['author']     = $this->input->post( 'author' );
			
			$config['id'] = $this->input->post( 'id' );
			
			if ( $this->zixun->update_zixun( $data, $config) ) {
				
				redirect( 'adminroot/zixun' );
				
			}

			
		}
	}
	
	
	public function del()
	{
		$id = intval( $this->uri->rsegment(4) );
		$this->zixun->del_zixun( $id );
		redirect( 'adminroot/zixun' );
	}
	
	/**
	 * 
	 * 表单验证
	 */
	private function _load_validation_rules()
	{
		$this->form_validation->set_error_delimiters('<li>', '</li>');
		$this->form_validation->set_rules('title','标题','required|xss_clean');

	}
}